<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 面对面建群</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
</head>
<body class="bg-light">
<div id="app" class="container">
    <div class="row justify-content-center mt-5">
        <div class="col-md-6">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">登录</h4>
                </div>
                <div class="card-body">
                    <form @submit.prevent="login">
                        <div class="mb-3">
                            <label for="username" class="form-label">用户名</label>
                            <input v-model="username" type="text" class="form-control" id="username" required>
                        </div>
                        <div class="mb-3">
                            <label for="password" class="form-label">密码</label>
                            <input v-model="password" type="password" class="form-control" id="password" required>
                        </div>
                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">登录</button>
                            <button type="button" class="btn btn-outline-secondary" @click="goToRegister">注册账号</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.37/dist/vue.global.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    const { createApp, ref } = Vue;

    createApp({
        setup() {
            const username = ref('');
            const password = ref('');

            const login = async () => {
                try {
                    const response = await axios.post('/api/auth/login', {
                        username: username.value,
                        password: password.value
                    });

                    if (response.data.code === 200) {
                        const data = response.data.data;
                        localStorage.setItem('token', data.token);
                        localStorage.setItem('user', JSON.stringify({
                            id: data.id,
                            username: data.username,
                            nickname: data.nickname
                        }));

                        window.location.href = '/index.html';
                    } else {
                        alert(response.data.message || '登录失败');
                    }
                } catch (error) {
                    alert('登录失败: ' + (error.response?.data?.message || error.message));
                }
            };

            const goToRegister = () => {
                window.location.href = '/register.html';
            };

            return {
                username,
                password,
                login,
                goToRegister
            };
        }
    }).mount('#app');
</script>
</body>
</html>
